package logic

import (
	"context"
	"doctor-medicine-system/doctorrpc/doctorrpc"
	"doctor-medicine-system/doctorrpc/internal/svc"
	"doctor-medicine-system/mysql"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type MedicineAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMedicineAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MedicineAddLogic {
	return &MedicineAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 药品添加
func (l *MedicineAddLogic) MedicineAdd(in *doctorrpc.MedicineAddRequest) (*doctorrpc.MedicineAddResponse, error) {
	// todo: add your logic here and delete this line

	var doctor mysql.Doctor
	if err := doctor.FindDoctor(mysql.DB, in.DoctorId); err != nil {
		return nil, errors.New("医生不存在")
	}

	var medicine = mysql.Medicine{
		MedicineName: in.MedicineName,
		Size:         in.Size,
		Where:        in.Where,
		Methods:      in.Methods,
		BadMethods:   in.BadMethods,
	}
	err := medicine.MedicineAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("添加失败")
	}
	return &doctorrpc.MedicineAddResponse{
		MedicineId: int64(medicine.ID),
	}, nil
}
